--Inport: PMEngine, PrivateDB, ProfileDB, GlobalDB
local PMEngine, L, PrivateDB, ProfileDB, GlobalDB = unpack(select(2, ...)); 
local CB = PMEngine:GetModule('CastBars')

local function getBar(info)
	return CB.CastBarTemplate.bars[info]
end

local getOpt, setOpt, getColor, setColor
do
	function getOpt(info)
		local key = info[#info] 
		return PMEngine.db.castbars[key]
	end
	
	function setOpt(info, value)
		local key = info[#info]
		PMEngine.db.castbars[key] = value
		CB:ApplySettings()
	end
	
	function getColor(info)
		return unpack(getOpt(info))
	end
	
	function setColor(info, r, g, b, a)
		setOpt(info, {r, g, b, a})
	end
end

PMEngine.Options.args.castbars = {
	type = "group",
	name = "CastBars",
	childGroups = "tree",
	args = {
		general = {
			order = 1,
			type = "group",
			inline = true,
			name = "General",
			args = {
				unlock = {
					type = "execute",
					name = "Toggle Bar Lock",
					desc = "Unlock the Bars to be able to move them around.",
					disabled = function() return PMEngine.db.castbars.embedToGrid end,
					func = function()
						CB:ToggleLock(true)
					end,
					order = 50,
				},
				nllock1 = {
					type = "description",
					name = "",
					order = 52,
				},
				--embedToGrid = {
				--	type = "toggle",
				--	name = "Grid2 Dock",
				--	desc = "Dock the castbars on top of Grid2 (Only enabled if Grid2 is Docked to your Actionbar)",
				--	get = function(info) return PMEngine.db.castbars.embedToGrid end,
				--	set = function(info, value)
				--		PMEngine.db.castbars.embedToGrid = value; StaticPopup_Show("CONFIG_RL") end,
				--	order = 53,
				--},
				nllock2 = {
					type = "description",
					name = "",
					order = 54,
				},
				casttimeprecision = {
					type = "range",
					name = "Cast Time Precision",							
					desc = "Number of decimals to show for the Cast Time",
					min = 0, max = 3, step = 1,
					get = getOpt,
					set = setOpt,
					order = 102,
				},
				colors = {
					type = "group",
					name = "Colors",
					desc = "Colors",
					guiInline = true,
					order = 450,
					get = getColor,
					set = setColor,
					args = {
						spelltextcolor = {
							type = "color",
							name = "Spell Text",
							desc = "Set the color of the Spell Text",
							order = 98,
						},
						timetextcolor = {
							type = "color",
							name = "Time Text",
							desc = "Set the color of the Time Text",
							order = 98,
						},
						header = {
							type = "header",
							name = "",
							order = 99,
						},
						castingcolor = {
							type = "color",
							name = "Casting",
							desc = "Set the color of the cast bar when Casting",
						},
						channelingcolor = {
							type = "color",
							name = "Channeling",
							desc = "Set the color of the cast bar when Channeling",
						},
						completecolor = {
							type = "color",
							name = "Complete",
							desc = "Set the color of the cast bar when Complete",
						},
						failcolor = {
							type = "color",
							name = "Failed",
							desc = "Set the color of the cast bar when Failed",
						},
						sparkcolor = {
							type = "color",
							name = "Spark Color",
							desc = "Set the color of the casting bar spark",
							hasAlpha = true,
						},
						nl1 = {
							type = "description",
							name = "",
							order = 101,
						},
					},
				},
			},
		},
		resetAll = {
			type = "execute",
			name = "Reset All",
			desc = "Reset all cast bad settings.",
			func = function()
				PMEngine.db.castbars = ProfileDB.castbars
				StaticPopup_Show("PRIVATE_RL")
				--CB:ToggleLock(true)
			end,
			order = 50,
		},
		
		-- player options
		player = {
			name = 'Player',
			type = 'group',
			order = 200,
			guiInline = false,
			get = function(info) 
				Values = CB:Merge(PMEngine.db.castbars.CastBarTemplateDefaults, PMEngine.db.castbars.player)
				return Values[ info[#info] ] 
			end,
			set = function(info, value) 
				PMEngine.db.castbars.player[ info[#info] ]  = value 
				CB.Player:UpdateDB()
				CB.Latency:ApplySettings()
				local bar = getBar('PlayerCastBar')
				bar.config[info[#info]] = value
				bar:ApplySettings()
			end,
			args = {
				enable = {
					type = "toggle",
					name = "Enable",
					desc = "Enable the Player Cast Bar.",
					get = function(info) return PMEngine.db.castbars.modules.Player end,
					set = function(info, value) PMEngine.db.castbars.modules.Player = value; StaticPopup_Show("CONFIG_RL") end,
					order = 99,
					width = "full",	
				},
				hideblizz = {
					type = "toggle",
					name = "Disable Blizzard Cast Bar",
					desc = "Disable and hide the default UI's casting bar",
					order = 101,
				},
				showticks = {
					type = "toggle",
					name = "Show channeling ticks",
					desc = "Show damage / mana ticks while channeling spells like Drain Life or Blizzard",
					order = 102,
				},
				targetname = {
					type = "toggle",
					name = "Show Target Name",
					desc = "Display target name of spellcasts after spell name",
					disabled = function() return PMEngine.db.castbars.player.hidenametext end,
					order = 402,
				},
				h = {
					type = "range",
					name = "Height",
					desc = "Height",
					min = 10, max = 50, step = 1,
					order = 200,
				},
				w = {
					type = "range",
					name = "Width",
					desc = "Width",
					min = 50, max = 1500, bigStep = 5,
					order = 200,
				},
				x = {
					type = "range",
					name = "X",
					desc = "Set an exact X value for this bar's position.",
					min = -2560, max = 2560, bigStep = 1,
					order = 200,
				},
				y = {
					type = "range",
					name = "Y",
					desc = "Set an exact Y value for this bar's position.",
					min = -1600, max = 1600, bigStep = 1,
					order = 200,
				},
				scale = {
					type = "range",
					name = "Scale",
					desc = "Scale",
					min = 0.2, max = 1, bigStep = 0.025,
					order = 201,
				},
				alpha = {
					type = "range",
					name = "Alpha",
					desc = "Alpha",
					isPercent = true,
					min = 0.1, max = 1, bigStep = 0.025,
					order = 202,
				},	
				fonthead = {
					type = "header",
					name = "Font and Text",
					order = 398,
				},
				font = {
					type = "select",
					dialogControl = "LSM30_Font",
					name = "Font",
					desc = "Set the font used in the Name and Time texts",
					values = AceGUIWidgetLSMlists.font,
					order = 399,
				},
				nlfont = {
					type = "description",
					name = "",
					order = 400,
				},
				hidenametext = {
					type = "toggle",
					name = "Hide Spell Name",
					desc = "Disable the text that displays the spell name",
					order = 401,
				},
				nlname = {
					type = "description",
					name = "",
					order = 403,
				},
				nametextposition = {
					type = "select",
					name = "Spell Name Position",
					desc = "Set the alignment of the spell name text",
					values = {["left"] = "Left", ["right"] = "Right", ["center"] = "Center"},
					--disabled = hidenametextoptions,
					order = 404,
				},
				fontsize = {
					type = "range",
					name = "Spell Name Font Size",
					desc = "Set the size of the spell name text",
					min = 7, max = 20, step = 1,
					order = 405,
					--disabled = hidenametextoptions,
				},
				timetextx = {
					type = "range",
					name = "Time Text X Offset",
					desc = "Adjust the X position of the time text",
					min = -35, max = 35, step = 1,
					--disabled = hidetimetextoptions,
					order = 416,
				},
				timetexty = {
					type = "range",
					name = "Time Text Y Offset",
					desc = "Adjust the Y position of the time text",
					min = -35, max = 35, step = 1,
					--disabled = hidetimetextoptions,
					order = 417,
				},
				--noInterruptGroup = {
				--	type = "group",
				--	name = "No interrupt cast bars",
				--	dialogInline = true,
				--	order = 455,
				--	args = {
				--		noInterruptChangeColor = {
				--			type = "toggle",
				--			name = "Change Color",
				--			desc = "Change the color of non-interruptible Cast Bars",
				--			order = 10,
				--		},
				--		noInterruptColor = {
				--			type = "color",
				--			name = "Cast Bar Color",
				--			desc = "Configure the color of the cast bar.",
				--			disabled = noInterruptChangeColor,
				--			--set = setColor,
				--			--get = getColor,
				--			order = 11,
				--		},
				--	},
				--},
				toolheader = {
					type = "header",
					name = "Tools",
					order = 500,
				},
				snaptocenter = {
					type = "select",
					name = "Snap to Center",
					desc = "Move the CastBar to center of the screen along the specified axis",
					get = false,
					--set = snapToCenter,
					values = {["horizontal"] = "Horizontal", ["vertical"] = "Vertical"},
					order = 503,
				},
				copysettings = {
					type = "select",
					name = "Copy Settings From",
					desc = "Select a bar from which to copy settings",
					get = false,
					--set = copySettings,
					values = {["Target"] = "Target", ["Player"] = "Player"},
					order = 504
				}
			},
		},

		target = {
			name = 'Target',
			type = 'group',
			order = 200,
			guiInline = false,
			get = function(info) 
				Values = CB:Merge(PMEngine.db.castbars.CastBarTemplateDefaults, PMEngine.db.castbars.target)
				return Values[ info[#info] ] 
			end,
			set = function(info, value) 
				PMEngine.db.castbars.target[ info[#info] ]  = value 
				local bar = getBar('TargetCastBar')
				bar.config[info[#info]] = value
				bar:ApplySettings()
			end,
			args = {
				enable = {
					type = "toggle",
					name = "Enable",
					desc = "Enable the Target Cast Bar.",
					get = function(info) return PMEngine.db.castbars.modules.Target end,
					set = function(info, value) PMEngine.db.castbars.modules.Target = value; StaticPopup_Show("CONFIG_RL") end,
					order = 99,
					width = "full",	
				},
				h = {
					type = "range",
					name = "Height",
					desc = "Height",
					min = 10, max = 50, step = 1,
					order = 200,
				},
				w = {
					type = "range",
					name = "Width",
					desc = "Width",
					min = 50, max = 1500, bigStep = 5,
					order = 200,
				},
				x = {
					type = "range",
					name = "X",
					desc = "Set an exact X value for this bar's position.",
					min = -2560, max = 2560, bigStep = 1,
					order = 200,
				},
				y = {
					type = "range",
					name = "Y",
					desc = "Set an exact Y value for this bar's position.",
					min = -1600, max = 1600, bigStep = 1,
					order = 200,
				},
				scale = {
					type = "range",
					name = "Scale",
					desc = "Scale",
					min = 0.2, max = 1, bigStep = 0.025,
					order = 201,
				},
				alpha = {
					type = "range",
					name = "Alpha",
					desc = "Alpha",
					isPercent = true,
					min = 0.1, max = 1, bigStep = 0.025,
					order = 202,
				},	
				fonthead = {
					type = "header",
					name = "Font and Text",
					order = 398,
				},
				font = {
					type = "select",
					dialogControl = "LSM30_Font",
					name = "Font",
					desc = "Set the font used in the Name and Time texts",
					values = AceGUIWidgetLSMlists.font,
					order = 399,
				},
				nlfont = {
					type = "description",
					name = "",
					order = 400,
				},
				hidenametext = {
					type = "toggle",
					name = "Hide Spell Name",
					desc = "Disable the text that displays the spell name",
					order = 401,
				},
				nlname = {
					type = "description",
					name = "",
					order = 403,
				},
				nametextposition = {
					type = "select",
					name = "Spell Name Position",
					desc = "Set the alignment of the spell name text",
					values = {["left"] = "Left", ["right"] = "Right", ["center"] = "Center"},
					--disabled = hidenametextoptions,
					order = 404,
				},
				fontsize = {
					type = "range",
					name = "Spell Name Font Size",
					desc = "Set the size of the spell name text",
					min = 7, max = 20, step = 1,
					order = 405,
					--disabled = hidenametextoptions,
				},
				timetextx = {
					type = "range",
					name = "Time Text X Offset",
					desc = "Adjust the X position of the time text",
					min = -35, max = 35, step = 1,
					--disabled = hidetimetextoptions,
					order = 416,
				},
				timetexty = {
					type = "range",
					name = "Time Text Y Offset",
					desc = "Adjust the Y position of the time text",
					min = -35, max = 35, step = 1,
					--disabled = hidetimetextoptions,
					order = 417,
				},
				noInterruptGroup = {
					type = "group",
					name = "No interrupt cast bars",
					dialogInline = true,
					order = 455,
					args = {
						noInterruptChangeColor = {
							type = "toggle",
							name = "Change Color",
							desc = "Change the color of non-interruptible Cast Bars",
							order = 10,
						},
						noInterruptColor = {
							type = "color",
							name = "Cast Bar Color",
							desc = "Configure the color of the cast bar.",
							--disabled = noInterruptChangeColor,
							set = function(info, r, g, b)
								PMEngine.db.castbars.target.noInterruptColor = {r, g, b}
								local bar = getBar('TargetCastBar')
								bar.config[info[#info]] = {r, g, b}
								bar:ApplySettings()
							end,
							get = function(info)
								local value = CB:Merge(PMEngine.db.castbars.CastBarTemplateDefaults, PMEngine.db.castbars.target)
								return unpack(value.noInterruptColor)
							end,
							order = 11,
						},
					},
				},
				toolheader = {
					type = "header",
					name = "Tools",
					order = 500,
				},
				snaptocenter = {
					type = "select",
					name = "Snap to Center",
					desc = "Move the CastBar to center of the screen along the specified axis",
					get = false,
					--set = snapToCenter,
					values = {["horizontal"] = "Horizontal", ["vertical"] = "Vertical"},
					order = 503,
				},
				copysettings = {
					type = "select",
					name = "Copy Settings From",
					desc = "Select a bar from which to copy settings",
					get = false,
					--set = copySettings,
					values = {["Target"] = "Target", ["Player"] = "Player"},
					order = 504
				}
			},
		},

		focus = {
			name = 'Focus',
			type = 'group',
			order = 203,
			guiInline = false,
			get = function(info) 
				Values = CB:Merge(PMEngine.db.castbars.CastBarTemplateDefaults, PMEngine.db.castbars.focus)
				return Values[ info[#info] ] 
			end,
			set = function(info, value) 
				PMEngine.db.castbars.focus[ info[#info] ]  = value 
				local bar = getBar('Focus')
				bar.config[info[#info]] = value
				bar:ApplySettings()
			end,
			args = {
				enable = {
					type = "toggle",
					name = "Enable",
					desc = "Enable the Focus Cast Bar.",
					get = function(info) return PMEngine.db.castbars.modules.Focus end,
					set = function(info, value) PMEngine.db.castbars.modules.Focus = value; StaticPopup_Show("CONFIG_RL") end,
					order = 99,
					width = "full",	
				},
				showfriendly = {
					type = "toggle",
					name = "Show for Friends",
					desc = "Show this castbar for friendly units",
					order = 101,
				},
				showhostile = {
					type = "toggle",
					name = "Show for Enemies",
					desc = "Show this castbar for hostile units",
					order = 101,
				},
				showtarget = {
					type = "toggle",
					name = "Show if Target",
					desc = "Show this castbar if focus is also target",
					order = 101,
				},
				h = {
					type = "range",
					name = "Height",
					desc = "Height",
					min = 10, max = 50, step = 1,
					order = 200,
				},
				w = {
					type = "range",
					name = "Width",
					desc = "Width",
					min = 50, max = 1500, bigStep = 5,
					order = 200,
				},
				x = {
					type = "range",
					name = "X",
					desc = "Set an exact X value for this bar's position.",
					min = -2560, max = 2560, bigStep = 1,
					order = 200,
				},
				y = {
					type = "range",
					name = "Y",
					desc = "Set an exact Y value for this bar's position.",
					min = -1600, max = 1600, bigStep = 1,
					order = 200,
				},
				scale = {
					type = "range",
					name = "Scale",
					desc = "Scale",
					min = 0.2, max = 1, bigStep = 0.025,
					order = 201,
				},
				alpha = {
					type = "range",
					name = "Alpha",
					desc = "Alpha",
					isPercent = true,
					min = 0.1, max = 1, bigStep = 0.025,
					order = 202,
				},	
				fonthead = {
					type = "header",
					name = "Font and Text",
					order = 398,
				},
				font = {
					type = "select",
					dialogControl = "LSM30_Font",
					name = "Font",
					desc = "Set the font used in the Name and Time texts",
					values = AceGUIWidgetLSMlists.font,
					order = 399,
				},
				nlfont = {
					type = "description",
					name = "",
					order = 400,
				},
				hidenametext = {
					type = "toggle",
					name = "Hide Spell Name",
					desc = "Disable the text that displays the spell name",
					order = 401,
				},
				nlname = {
					type = "description",
					name = "",
					order = 403,
				},
				nametextposition = {
					type = "select",
					name = "Spell Name Position",
					desc = "Set the alignment of the spell name text",
					values = {["left"] = "Left", ["right"] = "Right", ["center"] = "Center"},
					--disabled = hidenametextoptions,
					order = 404,
				},
				fontsize = {
					type = "range",
					name = "Spell Name Font Size",
					desc = "Set the size of the spell name text",
					min = 7, max = 20, step = 1,
					order = 405,
					--disabled = hidenametextoptions,
				},
				timetextx = {
					type = "range",
					name = "Time Text X Offset",
					desc = "Adjust the X position of the time text",
					min = -35, max = 35, step = 1,
					--disabled = hidetimetextoptions,
					order = 416,
				},
				timetexty = {
					type = "range",
					name = "Time Text Y Offset",
					desc = "Adjust the Y position of the time text",
					min = -35, max = 35, step = 1,
					--disabled = hidetimetextoptions,
					order = 417,
				},
				noInterruptGroup = {
					type = "group",
					name = "No interrupt cast bars",
					dialogInline = true,
					order = 455,
					args = {
						noInterruptChangeColor = {
							type = "toggle",
							name = "Change Color",
							desc = "Change the color of non-interruptible Cast Bars",
							order = 10,
						},
						noInterruptColor = {
							type = "color",
							name = "Cast Bar Color",
							desc = "Configure the color of the cast bar.",
							--disabled = noInterruptChangeColor,
							set = function(info, r, g, b)
								PMEngine.db.castbars.focus.noInterruptColor = {r, g, b}
								local bar = getBar('Focus')
								bar.config[info[#info]] = {r, g, b}
								bar:ApplySettings()
							end,
							get = function(info)
								local value = CB:Merge(PMEngine.db.castbars.CastBarTemplateDefaults, PMEngine.db.castbars.focus)
								return unpack(value.noInterruptColor)
							end,
							order = 11,
						},
					},
				},
				toolheader = {
					type = "header",
					name = "Tools",
					order = 500,
				},
				snaptocenter = {
					type = "select",
					name = "Snap to Center",
					desc = "Move the CastBar to center of the screen along the specified axis",
					get = false,
					--set = snapToCenter,
					values = {["horizontal"] = "Horizontal", ["vertical"] = "Vertical"},
					order = 503,
				},
				copysettings = {
					type = "select",
					name = "Copy Settings From",
					desc = "Select a bar from which to copy settings",
					get = false,
					--set = copySettings,
					values = {["Target"] = "Target", ["Player"] = "Player"},
					order = 504
				}
			},
		},

		--Latency Options
		latency = {
			type = "group",
			name = "Latency",
			order = 600,
			get = function(info) 
				Values = PMEngine.db.castbars.latency
				return Values[ info[#info] ] 
			end,
			set = function(info, value) 
				PMEngine.db.castbars.latency[ info[#info] ]  = value 
				CB.Latency:ApplySettings()
			end,
			args = {
				toggle = {
					type = "toggle",
					name = "Enable",
					desc = "Enable",
					get = function(info) return PMEngine.db.castbars.modules.Latency end,
					set = function(info, value) PMEngine.db.castbars.modules.Latency = value; StaticPopup_Show("CONFIG_RL") end,
					order = 100,
				},
				lagembed = {
					type = "toggle",
					name = "Embed",
					desc = "Include Latency time in the displayed cast bar.",
					order = 101,
				},
				lagalpha ={
					type = "range",
					name = "Alpha",
					desc = "Set the alpha of the latency bar",
					min = 0.05, max = 1, bigStep = 0.05,
					isPercent = true,
					order = 102,
				},
				lagpadding = {
					type = "range",
					name = "Embed Safety Margin",
					desc = "Embed mode will decrease it's lag estimates by this amount.  Ideally, set it to the difference between your highest and lowest ping amounts.  (ie, if your ping varies from 200ms to 400ms, set it to 0.2)",
					min = 0, max = 1, bigStep = 0.05,
					disabled = function()
						return not PMEngine.db.castbars.latency.lagembed
					end,
					order = 103,
				},
				lagcolor = {
					type = "color",
					name = "Bar Color",
					desc = "Set the color of the Latency Bar",
					get = getColor,
					set = setColor,
					set = function(info, r, g, b)
						PMEngine.db.castbars.latency.lagcolor = {r, g, b}
						CB.Latency:ApplySettings()
					end,
					get = function(info)
						return unpack(PMEngine.db.castbars.latency.lagcolor)
					end,
					order = 111,
				},
				header = {
					type = "header",
					name = "Font and Text",
					order = 113,
				},
				lagtext = {
					type = "toggle",
					name = "Show Text",
					desc = "Display the latency time as a number on the latency bar",
					order = 114,
				},
				lagtextcolor = {
					type = "color",
					name = "Text Color",
					desc = "Set the color of the latency text",
					set = function(info, r, g, b, a)
						PMEngine.db.castbars.latency.lagtextcolor = {r, g, b, a}
						CB.Latency:ApplySettings()
					end,
					get = function(info)
						return unpack(PMEngine.db.castbars.latency.lagtextcolor)
					end,
					disabled = function() return not PMEngine.db.castbars.latency.lagtext end,
					hasAlpha = true,
					order = 115,
				},
				lagfont = {
					type = "select",
					dialogControl = "LSM30_Font",
					name = "Font",
					desc = "Set the font used for the latency text",
					values = AceGUIWidgetLSMlists.font,
					disabled = function() return not PMEngine.db.castbars.latency.lagtext end,
					order = 116,
				},
				lagfontsize = {
					type = "range",
					name = "Font Size",
					desc = "Set the size of the latency text",
					min = 3, max = 15, step = 1,
					disabled = function() return not PMEngine.db.castbars.latency.lagtext end,
					order = 117,
				},
				lagtextalignment = {
					type = "select",
					name = "Text Alignment",
					desc = "Set the position of the latency text",
					values = {["center"] = "Center", ["left"] = "Left", ["right"] = "Right", ["outside"] = "Outside"},
					disabled = function() return not PMEngine.db.castbars.latency.lagtext end,
					order = 118,
				},
				lagtextposition = {
					type = "select",
					name = "Text Position",
					desc = "Set the vertical position of the latency text",
					values = {["above"] = "Above", ["top"] = "Top", ["bottom"] = "Bottom", ["below"] = "Below"},
					disabled = function() return not PMEngine.db.castbars.latency.lagtext end,
					order = 119,
				},
			},
		}
	}
}

